package com.the.ui.widge

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.the.ui.R
import com.the.ui.app.ext.drawText

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-11-05 09:36
 * @describe 变色文字
 * @email 625805189@qq.com
 * @remark
 */
open class ColorTrackTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {

    private var mOriginPaint: Paint? = null
    private var mChangePaint: Paint? = null
    private var mScalePaint: Paint? = null
    open var mCurrentProgress: Float = 0.5F
        set(value) {
            // 右边往左边变，先变化的是右边部分,这个百分比就得倒着算
            field = if (mDirection == Direction.LEFT_TO_RIGHT) value else 1 - value
            invalidate()
        }
    open var mDirection: Direction = Direction.LEFT_TO_RIGHT

    enum class Direction {
        LEFT_TO_RIGHT,
        RIGHT_TO_LEFT
    }

    init {
        attrs?.let {
            val typeArray = context.obtainStyledAttributes(it, R.styleable.ColorTrackTextView)
            val originColor =
                typeArray.getColor(R.styleable.ColorTrackTextView_originColor, Color.BLACK)
            val changeColor =
                typeArray.getColor(R.styleable.ColorTrackTextView_changeColor, Color.RED)

            typeArray.recycle()

            mOriginPaint = getPaintByColor(originColor)
            mChangePaint = getPaintByColor(changeColor)
            mScalePaint = getPaintByColor(changeColor,1.2F*textSize)

        }

    }

    private fun getPaintByColor(color: Int, size: Float = textSize): Paint {
        return Paint().apply {
            this.color = color
            // 抗锯齿
            isAntiAlias = true
            // 防抖动
            isDither = true
            textSize = size
        }
    }

    private var mScaleWidth = 10

    override fun onDraw(canvas: Canvas?) {
        val currentPoint = (mCurrentProgress * width).toInt()
        canvas?.let {
            // 从左到右
            if (mDirection == Direction.LEFT_TO_RIGHT) {
                // 绘制变色 0 -> currentPoint 部分
                it.drawText(mChangePaint, 0, currentPoint)
//                it.drawText(mScalePaint, getScaleWidth(currentPoint), currentPoint,true)
                // 绘制不变色 currentPoint -> width 部分
                it.drawText(mOriginPaint, currentPoint, width)
            } else {
                // 绘制变色 currentPoint -> width 部分
                it.drawText(mChangePaint, currentPoint, width)
                // 绘制不变色 0  -> currentPoint 部分
                it.drawText(mOriginPaint, 0, currentPoint)
            }
        }
    }

    private fun getScaleWidth(current:Int):Int{
        var width = current - mScaleWidth
        if(width < 0 )
            width == 0
        return width
    }

    private fun Canvas.drawText(paint: Paint?, start: Int, end: Int,isScale:Boolean = false) {
        if(start == 0 && end == 0) return
        paint?.let {
            // 保存当前状态
            save()
            // 裁剪出需要绘制的区域
            val rect = Rect(start, 0, end, height)
            clipRect(rect)
//            if(isScale){
//                scale(1.20f,1.2f,(end-start)/2.toFloat(),height/2.toFloat())
//            }
            // 绘制剪裁后区域的文字
            drawText(this, text.toString(), it)
            // 还原状态
            restore()
        }
    }

}

